/*
************************
* This jQuery extension is used to display data from the Battle Card System site.
* Written by Steve 'PyroZeroX' Andreou for http://www.battlecardsystem.com
* Usage: 
	$('#div').leaderboard({
		'ownerID' : <YOUR USER ID> (integer),
		'game' : <GAME ABBRIVATION>	(string)	
	});
	
	$('#div').stats({
		'playerID' : <A PLAYER ID> (integer)
	});
*
************************
*/
(function( $ ){
	$.fn.leaderboard= function( options ) {
		var $this = this;
		var settings = $.extend( {
			'ownerID' : null,
			'game' : null,
		}, options);
		if (settings['ownerID'] == null || settings['game'] == null) { return false; }
		var path = 'http://www.battlecardsystem.com/api/leaderboard/owner/'+settings['ownerID']+'/game/'+settings['game']+'/format/json';
		$this.append('<ol id="bcsleaderboard"></ol>');
		$.get(path, function(data) {
			$.each(data, function(key, player)
			{
				var str = "<li id='"+player.id+"' class='player'><a href='http://www.battlecardsystem.com/stats/view/"+player.id+"' target='_blank' class='playercard'>"+player.Name+ " [" + player.Fighter +"]</a> <br/><div class='playerdetails'>Wins: "+player.Wins+" Losses: "+player.Losses+"  BattlePoints: " + player.BattlePoints+" Rank: " + player.RankTitle +"</div></li>";
				$('#bcsleaderboard').append(str);
				$('#'+player.id+'').show('slow').delay(5000);
			});
		});
		
	};
	
	$.fn.stats = function( options )
	{
		var $this = this;
		var settings = $.extend({
			'playerID' : null
		}, options);
		
		if (settings['playerID'] == null) { return false };
		var path = 'http://www.battlecardsystem.com/api/stats/id/'+settings['playerID']+'/format/json';
		$.get(path, function(data)
		{
			var matchdata = data.matchdata;
			var playerdata = data.playerdata;
			var dispData = {
				"Wins" : thousandSeparator(playerdata.Wins, ','),
				"Losses" : thousandSeparator(playerdata.Losses, ','),
				"BattlePoints" : thousandSeparator(playerdata.BattlePoints, ','),
				"Streak" : thousandSeparator(playerdata.WinStreak, ','),
				"Best Streak" : thousandSeparator(playerdata.BestStreak, ','),
				"Rank" : playerdata.RankTitle,
				//"Rank" : "<img src='http://www.battlecardsystem.com/image/ranks/"+playerdata.IconURL+"' />",
				"Clean Sweeps" : thousandSeparator(playerdata.MajorWin, ','),
				"Destroyed" : thousandSeparator(playerdata.MajorLoss, ','),
				"Close Calls" : thousandSeparator(playerdata.MinorWin, ','),
				"Next Time..." : thousandSeparator(playerdata.MinorLoss, ','),
				"CodeID" : playerdata.CodeID
			};
			
			$this.append("<h2><a href='http://www.battlecardsystem.com/stats/view/"+playerdata.id+"'>"+playerdata.Name+" ["+playerdata.Fighter+"]</a></h2>");
			$this.append("<ul id='playerdata'></ul>");
			
			$.each(dispData, function(k, v)
			{
				$('#playerdata').append("<li>"+ v +"<p class='statlabel'>"+ k +"</p></li>");
			});
			$this.append("<br class='bcsclear'/>")
			$this.append("<table id='matchestable'>");
			$('#matchestable').append("<tr><th>ID</th><th>Winner</th><th>Loser</th><th>Margin</th><th>Points</th></tr>");
			$.each(matchdata, function(match, data)
			{
				if(data.Winner == playerdata.Name)
				{
					var cssclass = 'win';
				}
				else
				{
					var cssclass = 'lose';
				}
				$('#matchestable').append("<tr class='"+ cssclass +"'><td>"+data.ID+"</td><td>"+data.Winner+" ["+data.WinnerFighter+"]</td><td>"+data.Loser+" ["+data.LoserFighter+"]</td><td>"+data.Margin+"</td><td>"+data.Points+"</td></tr>");
			});
			$this.append("</table>");	
		});
	};

	//I did not write this function
	function thousandSeparator(n,sep) 
	{
		var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'),
		sValue=n+'';
		if (sep === undefined) {sep=',';}
		while(sRegExp.test(sValue)) 
		{
			sValue = sValue.replace(sRegExp, '$1'+sep+'$2');
		}
		return sValue;
	}
})( jQuery);